import { glTypeBytes } from "./Utils";


export interface IVtxFmtElemCfg {
    name: string;
    offset: number;
    type: number;
    num: number;
    normalize?: boolean;
}

export interface IVtxFmtElemInfo {
    name: string;
    offset: number;
    stride: number;
    stream: number;
    type: number;
    num: number;
    normalize: boolean;
    bytes: number;
}

export default class VertexFormatInfo {
    private _attr2el: { [id: string]: IVtxFmtElemInfo };
    private _elements: Array<IVtxFmtElemInfo>;
    private _bytes: number;
    private _hash: string;

    constructor(configs: Array<IVtxFmtElemCfg>) {
        this._attr2el = {};
        this._elements = [];
        this._bytes = 0;

        let hash = "";

        for (let i = 0, len = configs.length; i < len; ++i) {
            let cfg = configs[i];
            let el = {
                name: cfg.name,
                offset: this._bytes,
                stride: 0,
                stream: -1,
                type: cfg.type,
                num: cfg.num,
                normalize: (cfg.normalize === undefined) ? false : cfg.normalize,
                bytes: cfg.num * glTypeBytes(cfg.type),
            };

            this._attr2el[el.name] = el;
            this._bytes += el.bytes;
            hash += `${el.name}:${el.num}:${el.type}:${el.normalize}`;
        }

        for (let i = 0, len = this._elements.length; i < len; ++i) {
            let el = this._elements[i];
            el.stride = this._bytes;
        }

        // this._hash = murmurhash2(hash, 666);
    }

    element(attr:string) {
        return this._attr2el[attr];
    }

    getHash() {
        return this._hash;
    }
}